package cn.com.surker.system.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.dao.SysMenuRoleControllerDao;
import cn.com.surker.system.entity.SysMenuRoleController;
import cn.com.surker.system.service.MenuRoleControllerService;
import cn.com.surker.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author lucky
 * @create 2024年11月13 12:01
 * @desc
 */
@Service
public class MenuRoleControllerServiceImpl implements MenuRoleControllerService {

    @Resource
    private SysMenuRoleControllerDao menuRoleControllerDao;

    /**
     * 新增或修改权限
     * @param menuId 菜单
     * @param roleId 角色
     * @param type 类型 0 新增 1删除 2 修改
     * @param status 更新状态
     * @param creator 操作人
     * @return
     */
    public RestResultDto saveOrUpdateRole(Long menuId, Long roleId, Integer type, Integer status, String creator) {
        if (StringUtils.isAllParamNull(menuId, roleId, type, status))
            throw new MessageCenterException("参数错误");

        SysMenuRoleController menuRoleController = menuRoleControllerDao.findMenuRole(roleId, menuId, 0);

        Date nowDate = new Date();
        if (menuRoleController == null) {
            Integer isInsert = 0;
            Integer isUpdate = 0;
            Integer isDelete = 0;
            if (type == 0)
                isInsert = status;
            if (type == 1)
                isDelete = status;
            if (type == 2)
                isUpdate = status;
            menuRoleController = new SysMenuRoleController(menuId, roleId, isInsert, isUpdate, isDelete);
            menuRoleController.setCreateTime(nowDate);
            menuRoleController.setCreator(creator);
            Integer code = menuRoleControllerDao.insert(menuRoleController);
            if (code > 0)
                return RestResultDto.success("保存成功", menuRoleController);
        } else {
            if (type == 0)
                menuRoleController.setIsInsert(status);
            else if (type == 1)
                menuRoleController.setIsDelete(status);
            else if (type == 2)
                menuRoleController.setIsUpdate(status);
            menuRoleController.setUpdator(creator);
            menuRoleController.setUpdateTime(nowDate);
            Integer code = menuRoleControllerDao.updateById(menuRoleController);
            if (code > 0)
                return RestResultDto.success("更新成功", menuRoleController);
        }
        throw new MessageCenterException("保存失败");
    }


    /**
     * 根据用户名及菜单名称查询权限
     * @param uid
     * @param url
     * @return
     */
    public SysMenuRoleController findUidOrUrl(Long uid, String url) {
        if (StringUtils.isAllParamNull(uid, url)){
            SysMenuRoleController roleController = new SysMenuRoleController(0l,0l,0,0,0);
            return roleController;
        }
        SysMenuRoleController roleController = menuRoleControllerDao.findUidOrUrl(uid,url);
        if (roleController == null) {
            roleController = new SysMenuRoleController(0l,0l,0,0,0);
            return roleController;
        }
        return roleController;
    }
}
